package com.macro.mall.tiny.word;

import com.hankcs.hanlp.HanLP;
import com.hankcs.hanlp.seg.common.Term;

import java.util.*;

/**
 * @author: i_kun
 * @Desc: o_。
 * @create: 2025-06-26 21:51
 */

public class WordProcessor {
    // 分词 + 停用词过滤
    public static List<String> segment(String text, Set<String> stopWords) {
        List<Term> terms = HanLP.segment(text);
        List<String> words = new ArrayList<>();
        for (Term term : terms) {
            String word = term.word.trim();
            if (word.length() > 1 && !stopWords.contains(word)) {
                words.add(word);
            }
        }
        return words;
    }

    // 统计 Top-N 高频词
    public static Map<String, Integer> getTopWords(List<String> words, int topN) {
        Map<String, Integer> freqMap = new HashMap<>();
        for (String word : words) {
            freqMap.put(word, freqMap.getOrDefault(word, 0) + 1);
        }
        return freqMap.entrySet().stream()
                .sorted(Map.Entry.<String, Integer>comparingByValue().reversed())
                .limit(topN)
                .collect(LinkedHashMap::new, (map, entry) -> map.put(entry.getKey(), entry.getValue()), Map::putAll);
    }
}
